//
//  QSVistorLonginView.m
//  SinaWeiBo
//
//  Created by apple on 16/5/29.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QSVistorLonginView.h"

@interface QSVistorLonginView()
/**
 *  旋转的那圈小图案
 */
@property (nonatomic, strong) UIImageView* circleView;

/**
 *  旋转图案中间的图标
 */
@property (nonatomic, strong) UIImageView* iconView;

/**
 *  遮挡的view
 */
@property (nonatomic, strong) UIImageView* coverView;

/**
 *  文字介绍信息
 */
@property (nonatomic, strong) UILabel* textLable;

/**
 *  登录按钮
 */
@property (nonatomic, strong) UIButton* longinBtn;

/**
 *  注册按钮
 */
@property (nonatomic, strong) UIButton* registBtn;
@end

@implementation QSVistorLonginView

#pragma mark - 设置界面信息
- (void)setupInfoWithImageName:(NSString *)imageName andText:(NSString *)text
{
    // 设置属性的基本信息
    self.iconView.image = [UIImage imageNamed:imageName];
    self.textLable.text = text;
    
    // 判断circleView要不要隐藏
    if ([imageName isEqualToString:@"visitordiscover_feed_image_house"]) {
        // 让circleView和coverView显示并转起来
        self.circleView.hidden = NO;
        self.coverView.hidden = NO;
        [self startAnimation];
    }else{
        // 隐藏circleView和coverView
        self.circleView.hidden = YES;
        self.coverView.hidden = YES;
    }
}

/**
 *  让circleView转起来
 */
- (void)startAnimation
{
    // 创建一个旋转（transform.rotation）的动画对象
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    // 重复次数
    ani.repeatCount = CGFLOAT_MAX;
    // 旋转角度
    ani.toValue = @(2 * M_PI);
    // 旋转周期
    ani.duration = 20;
    
    // 当动画结束或者视图处于非活跃状态，动画不移除图层
    ani.removedOnCompletion = NO;
    
    // 为circleView添加动画效果
    [self.circleView.layer addAnimation:ani forKey:nil];
}

/**
 *  重写构造函数
 */
- (instancetype)init
{
    //view的大小  在 viewDidLoad就会设置
    self = [super initWithFrame:CGRectZero];
    
    // 设置访客视图的界面
    [self setupUI];
    
    return self;
}

/**
 *  设置访客视图的界面
 */
- (void)setupUI
{
    // 添加子控件
    [self addSubview:self.circleView];
    [self addSubview:self.iconView];
    [self addSubview:self.coverView];
    [self addSubview:self.textLable];
    
    [self addSubview:self.longinBtn];
    [self addSubview:self.registBtn];
    
    // 添加按钮点击事件
    [self.longinBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.registBtn addTarget:self action:@selector(registBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 设置背景颜色
    [self setBackgroundColor:[UIColor colorWithWhite:0.93 alpha:1]];
    
    // 对子控件进行布局
    [self setupSubviewsFrame];
}

/**
 *  点击登录按钮
 */
- (void)loginBtnClick
{
    QSLog(@"登录");
    // 判断代理方法是否存在
    if ([self.delegate respondsToSelector:@selector(visitorWillLogin)]) {
        [self.delegate visitorWillLogin];
    }
}

/**
 *  点击注册按钮
 */
- (void)registBtnClick
{
    QSLog(@"注册");
    if ([self.delegate respondsToSelector:@selector(visitorWillRegist)]) {
        [self.delegate visitorWillRegist];
    }
}

/**
 *  对子控件进行布局
 */
- (void)setupSubviewsFrame
{
    // 关闭autoResize（因为autoLayout和autoResize不能共存）
    for (UIView *view in self.subviews) {
        view.translatesAutoresizingMaskIntoConstraints = false;
    }
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.circleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.circleView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-50]];
    
    // iconView的约束
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    
    // textLable的约束
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.textLable attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.textLable attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:26]];
    [_textLable addConstraint:[NSLayoutConstraint constraintWithItem:self.textLable attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50]];
    [_textLable addConstraint:[NSLayoutConstraint constraintWithItem:self.textLable attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:224]];
    
    // longinBtn的约束
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.longinBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.textLable attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.longinBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.textLable attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [_longinBtn addConstraint:[NSLayoutConstraint constraintWithItem:self.longinBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:35]];
    [_longinBtn addConstraint:[NSLayoutConstraint constraintWithItem:self.longinBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    
    // registBtn的约束
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.textLable attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.textLable attribute:NSLayoutAttributeBottom multiplier:1.0 constant:16]];
    [_registBtn addConstraint:[NSLayoutConstraint constraintWithItem:self.registBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:35]];
    [_registBtn addConstraint:[NSLayoutConstraint constraintWithItem:self.registBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100]];
    
    // coverView的约束
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[coverView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{@"coverView":_coverView}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[coverView]-(-45)-[registBtn]" options:0 metrics:nil views:@{@"coverView":_coverView, @"registBtn":_registBtn}]];
}

#pragma mark - 懒加载所有子控件
// 旋转的那圈小图案
- (UIImageView *)circleView
{
    if (!_circleView) {
        // 创建控件
        _circleView = [[UIImageView alloc] init];
        _circleView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
    }
    return _circleView;
}

// 旋转图案中间的图标
- (UIImageView *)iconView
{
    if (!_iconView) {
        // 创建控件
        _iconView = [[UIImageView alloc] init];
    }
    return _iconView;
}

// 遮挡的view
- (UIImageView *)coverView
{
    if (!_coverView) {
        // 创建控件
        _coverView = [[UIImageView alloc] init];
        _coverView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
    }
    return _coverView;
}

// 文字介绍信息
- (UILabel *)textLable
{
    if (!_textLable) {
        _textLable = [[UILabel alloc] init];
        
        // 设置lable的字体和颜色
        _textLable.textAlignment = NSTextAlignmentCenter;
        _textLable.font = [UIFont systemFontOfSize:14];
        _textLable.textColor = [UIColor lightGrayColor];
        
        // 设置自动换行
        _textLable.numberOfLines = 0;
    }
    
    return _textLable;
}

// 登录按钮
- (UIButton *)longinBtn
{
    if (!_longinBtn) {
        _longinBtn = [[UIButton alloc] init];
        
        // 设置按钮的属性
        [_longinBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_longinBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_longinBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    return _longinBtn;
}

// 注册按钮
- (UIButton *)registBtn
{
    if (!_registBtn) {
        _registBtn = [[UIButton alloc] init];
        
        // 设置按钮的属性
        [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_registBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    }
    return _registBtn;
}
@end
